package Dialog;

import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import utils.Parameters;
import view.MainFrame;

/**
 * 
 * @author Maxime
 */
public class ParametersDialog extends javax.swing.JDialog {

	private UIManager.LookAndFeelInfo[] m_lookAndFeelInfo;


	/**
	 * A return status code - returned if Cancel button has been pressed
	 */
	public static final int RET_CANCEL = 0;
	/**
	 * A return status code - returned if OK button has been pressed
	 */
	public static final int RET_OK = 1;

	/**
	 * Creates new form ParametersDialog
	 */
	public ParametersDialog(java.awt.Frame parent, boolean modal) {
		super(parent, modal);
		initComponents();

		// Close the dialog when Esc is pressed
		String cancelName = "cancel";
		InputMap inputMap = getRootPane().getInputMap(
				JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
		inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), cancelName);
		ActionMap actionMap = getRootPane().getActionMap();
		actionMap.put(cancelName, new AbstractAction() {

			public void actionPerformed(ActionEvent e) {
				doClose(RET_CANCEL);
			}
		});
	}

	/**
	 * @return the return status of this dialog - one of RET_OK or RET_CANCEL
	 */
	public int getReturnStatus() {
		return returnStatus;
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {
		m_lookAndFeelInfo = UIManager.getInstalledLookAndFeels();

		m_okButton = new javax.swing.JButton();
		m_cancelButton = new javax.swing.JButton();
		javax.swing.JLabel jLabel1 = new javax.swing.JLabel();
		javax.swing.JSeparator jSeparator1 = new javax.swing.JSeparator();
		javax.swing.JLabel jLabel2 = new javax.swing.JLabel();
		javax.swing.JLabel jLabel3 = new javax.swing.JLabel();
		javax.swing.JLabel jLabel4 = new javax.swing.JLabel();
		javax.swing.JLabel jLabel5 = new javax.swing.JLabel();
		javax.swing.JLabel jLabel6 = new javax.swing.JLabel();
		m_spinnerWidth = new javax.swing.JSpinner();
		javax.swing.JLabel jLabel7 = new javax.swing.JLabel();
		m_spinnerHeight = new javax.swing.JSpinner();
		m_comboBoxStyle = new javax.swing.JComboBox();
		m_spinnerGrid = new javax.swing.JSpinner();
		m_spinnerPreview = new javax.swing.JSpinner();
		m_spinnerSpray = new javax.swing.JSpinner();
		javax.swing.JSeparator jSeparator2 = new javax.swing.JSeparator();

		addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent evt) {
				closeDialog(evt);
			}
		});

		m_okButton.setText("OK");
		m_okButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				m_okButtonActionPerformed(evt);
			}
		});

		m_cancelButton.setText("Cancel");
		m_cancelButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				m_cancelButtonActionPerformed(evt);
			}
		});

		jLabel1.setText("Settings");

		jLabel2.setText("Look and feel:");

		jLabel3.setText("Grid size:");

		jLabel4.setText("Layer preview size:");

		jLabel5.setText("Spray Density:");

		jLabel6.setText("Default new Document size:");

		jLabel7.setText("x");

		m_spinnerWidth.setValue(Parameters.DOCUMENT_DEFAULT_WIDTH);
		m_spinnerHeight.setValue(Parameters.DOCUMENT_DEFAULT_HEIGHT);
		m_spinnerGrid.setValue(Parameters.GRID_SIZE);
		m_spinnerSpray.setValue(Parameters.SPRAY_DENSITY);
		m_spinnerPreview.setValue(Parameters.PREVIEW_SIZE);
		

		String[] lookAndFeelNames = new String[m_lookAndFeelInfo.length];
		int m_currentLookAndFeelIndex = 0;
		for (int i = 0; i < m_lookAndFeelInfo.length; i++) {
			lookAndFeelNames[i] = (m_lookAndFeelInfo[i].getName());
			if ( m_lookAndFeelInfo[i].getClassName().equals(Parameters.LOOK_AND_FEEL))
				m_currentLookAndFeelIndex = i;
		}
		
		m_comboBoxStyle.setModel(new javax.swing.DefaultComboBoxModel(
				lookAndFeelNames));
		m_comboBoxStyle.setSelectedIndex(m_currentLookAndFeelIndex);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addGroup(
														layout.createSequentialGroup()
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING)
																				.addComponent(
																						jSeparator1)
																				.addGroup(
																						javax.swing.GroupLayout.Alignment.TRAILING,
																						layout.createSequentialGroup()
																								.addGap(0,
																										161,
																										Short.MAX_VALUE)
																								.addComponent(
																										m_okButton,
																										javax.swing.GroupLayout.PREFERRED_SIZE,
																										67,
																										javax.swing.GroupLayout.PREFERRED_SIZE)
																								.addPreferredGap(
																										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																								.addComponent(
																										m_cancelButton))
																				.addGroup(
																						layout.createSequentialGroup()
																								.addGroup(
																										layout.createParallelGroup(
																												javax.swing.GroupLayout.Alignment.LEADING)
																												.addComponent(
																														jLabel1)
																												.addGroup(
																														layout.createSequentialGroup()
																																.addGroup(
																																		layout.createParallelGroup(
																																				javax.swing.GroupLayout.Alignment.TRAILING)
																																				.addComponent(
																																						jLabel2)
																																				.addComponent(
																																						jLabel6)
																																				.addComponent(
																																						jLabel3)
																																				.addComponent(
																																						jLabel4)
																																				.addComponent(
																																						jLabel5))
																																.addPreferredGap(
																																		javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
																																.addGroup(
																																		layout.createParallelGroup(
																																				javax.swing.GroupLayout.Alignment.LEADING)
																																				.addGroup(
																																						layout.createParallelGroup(
																																								javax.swing.GroupLayout.Alignment.LEADING,
																																								false)
																																								.addGroup(
																																										layout.createSequentialGroup()
																																												.addComponent(
																																														m_spinnerWidth,
																																														javax.swing.GroupLayout.PREFERRED_SIZE,
																																														58,
																																														javax.swing.GroupLayout.PREFERRED_SIZE)
																																												.addPreferredGap(
																																														javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																																												.addComponent(
																																														jLabel7)
																																												.addPreferredGap(
																																														javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																																												.addComponent(
																																														m_spinnerHeight,
																																														javax.swing.GroupLayout.PREFERRED_SIZE,
																																														53,
																																														javax.swing.GroupLayout.PREFERRED_SIZE))
																																								.addComponent(
																																										m_comboBoxStyle,
																																										0,
																																										javax.swing.GroupLayout.DEFAULT_SIZE,
																																										Short.MAX_VALUE))
																																				.addGroup(
																																						layout.createParallelGroup(
																																								javax.swing.GroupLayout.Alignment.TRAILING,
																																								false)
																																								.addComponent(
																																										m_spinnerSpray,
																																										javax.swing.GroupLayout.Alignment.LEADING,
																																										javax.swing.GroupLayout.DEFAULT_SIZE,
																																										53,
																																										Short.MAX_VALUE)
																																								.addComponent(
																																										m_spinnerPreview,
																																										javax.swing.GroupLayout.Alignment.LEADING)
																																								.addComponent(
																																										m_spinnerGrid,
																																										javax.swing.GroupLayout.Alignment.LEADING)))))
																								.addGap(0,
																										0,
																										Short.MAX_VALUE)))
																.addContainerGap())
												.addComponent(
														jSeparator2,
														javax.swing.GroupLayout.Alignment.TRAILING))));

		layout.linkSize(javax.swing.SwingConstants.HORIZONTAL,
				new java.awt.Component[] { m_cancelButton, m_okButton });

		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addComponent(jLabel1,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										14,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(jSeparator1,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										10,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.TRAILING)
												.addComponent(jLabel6)
												.addGroup(
														layout.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
																.addComponent(
																		m_spinnerWidth,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		javax.swing.GroupLayout.DEFAULT_SIZE,
																		javax.swing.GroupLayout.PREFERRED_SIZE)
																.addComponent(
																		jLabel7)
																.addComponent(
																		m_spinnerHeight,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		javax.swing.GroupLayout.DEFAULT_SIZE,
																		javax.swing.GroupLayout.PREFERRED_SIZE)))
								.addGap(18, 18, 18)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(jLabel2)
												.addComponent(
														m_comboBoxStyle,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addGap(18, 18, 18)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(jLabel3)
												.addComponent(
														m_spinnerGrid,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addGap(18, 18, 18)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(jLabel4)
												.addComponent(
														m_spinnerPreview,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addGap(18, 18, 18)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(jLabel5)
												.addComponent(
														m_spinnerSpray,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)
								.addComponent(jSeparator2,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										10,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(m_cancelButton)
												.addComponent(m_okButton))
								.addGap(27, 27, 27)));

		getRootPane().setDefaultButton(m_okButton);

		pack();
		this.setLocationRelativeTo(getParent());
	}// </editor-fold>

	private void m_okButtonActionPerformed(java.awt.event.ActionEvent evt) {

		Parameters.DOCUMENT_DEFAULT_WIDTH = (Integer) m_spinnerWidth.getValue();
		Parameters.DOCUMENT_DEFAULT_HEIGHT = (Integer) m_spinnerHeight
				.getValue();
		Parameters.GRID_SIZE = (Integer) m_spinnerGrid.getValue();
		Parameters.SPRAY_DENSITY = (Integer) m_spinnerSpray.getValue();
		Parameters.PREVIEW_SIZE = (Integer) m_spinnerPreview.getValue();
		
		int i = m_comboBoxStyle.getSelectedIndex();
		Parameters.LOOK_AND_FEEL = m_lookAndFeelInfo[i].getClassName();
		try {
			UIManager.setLookAndFeel(m_lookAndFeelInfo[i].getClassName());
			
			// refresh all components to display with new Look and Feel
			SwingUtilities.updateComponentTreeUI(MainFrame.getInstance());
			System.out.println(m_lookAndFeelInfo[i].getClassName());
			MainFrame.getInstance().repaint();
		} catch (Exception ex) {
			System.err.println("Error" + ex.getMessage());
		}
		doClose(RET_OK);
	}

	private void m_cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {
		doClose(RET_CANCEL);
	}

	/**
	 * Closes the dialog
	 */
	private void closeDialog(java.awt.event.WindowEvent evt) {
		doClose(RET_CANCEL);
	}

	private void doClose(int retStatus) {
		returnStatus = retStatus;
		setVisible(false);
		dispose();
	}

	// Variables declaration - do not modify
	private javax.swing.JButton m_cancelButton;
	private javax.swing.JComboBox m_comboBoxStyle;
	private javax.swing.JButton m_okButton;
	private javax.swing.JSpinner m_spinnerGrid;
	private javax.swing.JSpinner m_spinnerHeight;
	private javax.swing.JSpinner m_spinnerPreview;
	private javax.swing.JSpinner m_spinnerSpray;
	private javax.swing.JSpinner m_spinnerWidth;
	// End of variables declaration
	private int returnStatus = RET_CANCEL;

}
